package cn.vendsystem.com.service.impl;

import cn.vendcommon.com.entity.Machine;
import cn.vendcommon.com.entity.Result;
import cn.vendsystem.com.mapper.InventoryMapper;
import cn.vendsystem.com.mapper.MachineMapper;
import cn.vendsystem.com.service.MachineService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MachineServiceImpl implements MachineService {

    @Resource
    private MachineMapper machineMapper;
    @Resource
    private InventoryMapper inventoryMapper;

    @Override
    public Machine addMachine(Machine machine) {
        int rows = machineMapper.insert(machine);
        return rows > 0 ? machine : null;
    }

    @Override
    public int deleteMachine(int id) {
        return machineMapper.deleteById(id);
    }

    @Override
    public Machine updateMachine(Machine machine) {
        int rows = machineMapper.update(machine);
        return rows > 0 ? machine : null;
    }

    @Override
    public Machine getMachineById(int id) {
        Machine machine = machineMapper.selectById(id);
        if (machine != null) {
            machine.setInventory(inventoryMapper.selectInventoryByMachineId(machine.getMachineId()));
        }
        return machine != null ? machine : null;
    }

    @Override
    public List<Machine> getAllMachines() {
        return machineMapper.selectAll();
    }
}
